package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.question.QuestionTransfer;
import com.admin.education.dataTransferObject.question.Request.CreateQuestionRequest;
import com.admin.education.dataTransferObject.question.Request.QueryQuestionRequest;
import com.admin.education.dataTransferObject.question.Request.UpdateQuestionRequest;
import com.admin.education.services.Interfaces.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@JwtAuthorize
@RestController
@RequestMapping("questions")
@CrossOrigin(origins = "*", maxAge = 3600)
public class QuestionController {
    @Autowired
    private IQuestionService questionService;
    @GetMapping("")
    public Callable<IResponseMessage> queryQuestions(@Valid QueryQuestionRequest queryClassesRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<PageResponse<QuestionTransfer>> servicesExcuteResult = questionService.queryQuestions(queryClassesRequest);

                if (servicesExcuteResult.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<PageResponse<QuestionTransfer>>(Result.Success, servicesExcuteResult.getData());
                } else {
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @GetMapping("{questionId}")
    public Callable<IResponseMessage> queryQuestionsById(@PathVariable Long questionId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<QuestionTransfer> result = questionService.queryQuestionById(questionId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<QuestionTransfer>(Result.Success, result.getData());
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PostMapping("")
    public Callable<IResponseMessage> createQuestions(@RequestBody CreateQuestionRequest createQuestionRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = questionService.createQuestion(createQuestionRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @DeleteMapping("{questionId}")
    public Callable<IResponseMessage> deleteClassesById(@PathVariable Long questionId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = questionService.deleteQuestion(questionId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PutMapping("{questionId}")
    public Callable<IResponseMessage> updateQuestions(@PathVariable Long questionId, @RequestBody @Valid UpdateQuestionRequest updateQuestionRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = questionService.updateQuestion(questionId, updateQuestionRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

}
